package com.hddznet.uniplatform.sm.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hddznet.uniplatform.sm.entity.SysDictEntity;
import com.hddznet.uniplatform.sm.entity.SysDictItemEntity;
import com.hddznet.uniplatform.sm.service.ISysDictService;
import com.hddznet.uniplatform.sm.util.HdCustomQueryUtils;
import com.hddznet.uniplatform.sm.service.ISysDictItemService;
import com.hddznet.uniplatform.sm.vo.Result;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/sys/dict")
@Slf4j
public class SysDictController {
	
	@Autowired
	private ISysDictService sysDictService;
	@Autowired
	private ISysDictItemService sysDictItemService;
	
	/**
	 * 分页列表查询
	 *
	 * @param sysDictEntity
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/list")
	public Result<?> queryPageList(SysDictEntity sysDictEntity,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<SysDictEntity> queryWrapper = HdCustomQueryUtils.initQueryWrapper(sysDictEntity, req.getParameterMap());
		//QueryWrapper<SysDictEntity> queryWrapper = null;
		Page<SysDictEntity> page = new Page<SysDictEntity>(pageNo, pageSize);
		IPage<SysDictEntity> pageList = sysDictService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 * 添加
	 *
	 * @param sysDictEntityPage
	 * @return
	 */
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody SysDictEntity sysDictEntity) {
		sysDictService.saveMain(sysDictEntity, sysDictEntity.getSysDictItemEntityList());
		return Result.ok(sysDictEntity);
	}
	
	/**
	 * 编辑
	 *
	 * @param sysDictEntityPage
	 * @return
	 */
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody SysDictEntity sysDictEntity) {
		BeanUtils.copyProperties(sysDictEntity, sysDictEntity);
		sysDictService.updateMain(sysDictEntity, sysDictEntity.getSysDictItemEntityList());
		return Result.ok("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
	    sysDictService.delMain(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.sysDictService.delBatchMain(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}
	
	/**
	 * 通过id查询
     *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		SysDictEntity sysDictEntity = sysDictService.getById(id);
		return Result.ok(sysDictEntity);
	}
	
	/**
	 * 通过id查询
     *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/querySysDictItemEntityByMainId")
	public Result<?> querySysDictItemEntityListByMainId(@RequestParam(name="id",required=true) String id) {
		List<SysDictItemEntity> sysDictItemEntityList = sysDictItemService.selectByMainId(id);
		return Result.ok(sysDictItemEntityList);
	}

}
